﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="Assign_Roles2.aspx.cs" Inherits="BOComplementare.admin.access.Assign_Roles2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript" lang="javascript" src="/Scripts/GrayParrot.Core.js"></script> 
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <!-- 
        Sitemap Menu item:
        <siteMapNode url="~/Assign_Roles2.aspx" title="Assign_Roles2" description="Assign_Roles2" image="fa-laptop" id="Assign_Roles2"/>
    -->
    <section class="panel">
	    <header class="panel-heading">
			<h2>Assign Roles</h2>
		</header>
<%--		<div class="panel-tools" align="right">
			<ul class="tooltip-area">
				<li><a href="javascript:void(0)" class="btn btn-collapse" title="Collapse"><i class="fa fa-sort-amount-asc"></i></a></li>
				<li><a href="javascript:void(0)" class="btn btn-reload"  title="Reload"><i class="fa fa-retweet"></i></a></li>
				<li><a href="javascript:void(0)" class="btn btn-close" title="Close"><i class="fa fa-times"></i></a></li>
			</ul>
		</div>--%>
		<div class="panel-body">

<%--            <table border="1" class="table table-bordered table-striped table-condensed">
                <tr>
                    <td>
            		        <div class="ios-switch theme-inverse pull-right">
									<div class="switch"><input type="checkbox" id="option_1" name="option_1" checked /></div>
							</div>
                    </td>
                </tr>
                </table>--%>
            <!-- PUT YOUR CONTENT HERE -->

            <div id="divModifiche" style="display:none"></div> 
            <button type="button" class="btn" onclick="ShowModifiche()">Visualizza Modifiche</button>
            <div id="assignedPagesRoles"></div> 

		</div>
		<footer class="panel-footer">
			<button type="submit" class="btn" data-color="#F4AD41" onclick="Salva()">Salva</button>
			<button type="reset" class="btn" onclick="Reset()">Reset</button>
		</footer>
	</section>

</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="LocaJavascript" runat="server">
    <script type="text/javascript">
        var m_pageInRole = {};
        m_pageInRole.PageName = '';
        m_pageInRole.RoleName = '';
        m_pageInRole.Checked = false;

        var m_pagesInRoles = [];

        $(document).ready(function () {

            LoadPagesRolesMatrix();

        });

        /* hanlde dell'evento change sui controlli di tipo checkbox aggiunti dinamicamente alla pagina */
        $(document).on('change', "input[type='checkbox']", function () {

            var m_elem = this.id.split("_");

            var m_pageInRole = {};
            m_pageInRole.PageName = m_elem[0];
            m_pageInRole.RoleName = m_elem[1];
            m_pageInRole.Checked = this.checked;

            var index = inArray(m_pageInRole, m_pagesInRoles);
            if (index == -1) {
                m_pagesInRoles.push(m_pageInRole);
            } else {
                if (index != -1) {
                    m_pagesInRoles[index] = m_pageInRole;
                } else {
                    m_pagesInRoles.push(m_pageInRole);
                }
            }
        });

        function inArray(item, arr) {
            var index = -1;

            $.each(arr, function (i, p) {
                if ((item.PageName == p.PageName) &&
                    (item.RoleName == p.RoleName)) {
                    index = i;
                    return false;
                }
            });

            return index; 
        }

        function ShowModifiche() {
            ArrayToTable('divModifiche', m_pagesInRoles);

            if (m_pagesInRoles.length == 0) {
                ShowMessage("Nessuna modifica", 1);
                return;
            }

            $("#divModifiche").dialog({
                resizable: false,
                modal: true,
                title: "Modifiche Pagine - Ruoli",
                height: 250,
                width: 400
            });
        }

        function Salva() {
            if (m_pagesInRoles.length == 0) {
                ShowMessage("Nessuna modifica", 1);
                return;
            }

            var data = JSON.stringify({ data: m_pagesInRoles })
            
            $.ajax({
                type: "POST",
                url: '/api/authentication/UpdatePagesInRoles',
                data: data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var data = JSON.parse(response);

                    if (data.d) {
                        ShowMessage(data.msg, 0);
                    } else {
                        ShowMessage(data.msg, 3); 
                    }
                },
                failure: function (response) {
                    var data = JSON.parse(response);

                    ShowMessage(data.msg, 3);
                }
            });


        }

        function Reset() {
            m_pagesInRoles.length = 0;

            LoadPagesRolesMatrix();
        }

        function LoadPagesRolesMatrix() {

            $.ajax({
                type: "GET",
                url: '/api/authentication/LoadPagesInRoles',
                data: null,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {

                    var data = JSON.parse(response);

                    $('#assignedPagesRoles').empty();

                    var table = MakeAssignationTableStructure(data[0]);

                    LoadTableData(table, data);
                },
                failure: function (response) {
                    alert(response.d);
                }
            });
        }

        function MakeAssignationTableStructure(data) {

            var m_table = $('<table id="matrixPagesRoles" class="table table-bordered table-striped table-condensed"></table>');
            
            var m_headers = $('<tr>');
            $.each(data, function (key, item) {
                $('<td>' + key + '</td>').appendTo(m_headers);
            });
            $('</tr>').appendTo(m_headers);

            $(m_headers).appendTo(m_table);
            $(m_table).appendTo($('#assignedPagesRoles'));

            return m_table;
        }

        function LoadTableData(table, data) {

            $.each(data, function (index, item) {

                var m_rows = $('<tr>');
                var m_pageName = '';

                $.each(item, function (key, prop) {

                    if (key != 'PageName') {

                        if ((prop !== undefined) && (prop != null) && (prop >= 1)) {
                            var pp = GetCheckBox(m_pageName + '_' + key, m_pageName + '_' + key, 'checked');
                        } else {
                            var pp = GetCheckBox(m_pageName + '_' + key, m_pageName + '_' + key);
                        }

                        $(pp).appendTo(m_rows);
                    } else {
                        m_pageName = prop;

                        $('<td>' + prop + '</td>').appendTo(m_rows);
                    }

                });

                $('</tr>').appendTo(m_rows);

                //
                $(m_rows).appendTo(table);

            });
           
        }

        function GetCheckBox(key, esito, checked) {

            //return '<td><div class="ios-switch theme pull-right"><div class="switch"><input id=\'' + key + '\' name=\'' + esito + '\' type=\'checkbox\' ' + checked + '/></div></div></td>';
            
            return '<td><input id=\'' + key + '\' name=\'' + esito + '\' type=\'checkbox\' ' + checked + '/></td>';
        }
    </script>
</asp:Content>